package sau.diyizu.media.video;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.widget.SeekBar;
import android.widget.Toast;
import android.widget.VideoView;

import sau.diyizu.media.R;

/**
 @author JYD
 @version 1.0
 @since 2020/12/15 0:20 */
public class VideoActivity extends Activity {
    public static final String VIDEO_PATH = "video_path";

    public static Intent createIntent(Context context, String videoPath) {
        Intent intent = new Intent(context, VideoActivity.class);
        intent.putExtra(VIDEO_PATH, videoPath);
        return intent;
    }

    private VideoView content_vv;
    private SeekBar playProgress_sb;
    private View left_v;
    private View center_v;
    private View right_v;

    @Override
    protected void onCreate(Bundle saveInstanceState) {
        super.onCreate(saveInstanceState);
        setContentView(R.layout.activity_video);
        onBindView();
        onBindListener();
        String path = getIntent().getStringExtra(VIDEO_PATH);
        Uri uri = Uri.parse(path);

        content_vv.setVideoURI(uri);
        MediaPlayer mediaPlayer = MediaPlayer.create(this, Uri.parse(path));
        playProgress_sb.setMax(mediaPlayer.getDuration());
        startPlay();
    }

    private void onBindView() {
        content_vv = findViewById(R.id.videoView);
        playProgress_sb = findViewById(R.id.play_progress_sb);
        left_v = findViewById(R.id.left_v);
        center_v = findViewById(R.id.center_v);
        right_v = findViewById(R.id.right_v);
    }

    private void onBindListener() {
        left_v.setOnClickListener(v -> {
            content_vv.seekTo(0);
            playProgress_sb.setProgress(0);
        });
        center_v.setOnClickListener(v -> {
            if (!center_v.isSelected()) {
                startPlay();
            } else {
                content_vv.pause();
                center_v.setSelected(false);
                playProgress_sb.setProgress(content_vv.getCurrentPosition());
            }
        });
        right_v.setOnClickListener(v -> toast("这玩意没啥用！"));
        playProgress_sb.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) { }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) { }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {
                content_vv.seekTo(seekBar.getProgress());
            }
        });
        content_vv.setOnCompletionListener(mp -> {
            center_v.setSelected(false);
            playProgress_sb.setProgress(content_vv.getCurrentPosition());
        });
    }

    private void startPlay() {
        content_vv.start();
        center_v.setSelected(true);
        startAsyncUpdateProgress();
    }

    private void startAsyncUpdateProgress() {
        UpdateProgressTask task = new UpdateProgressTask();
        task.execute();
    }

    private class UpdateProgressTask extends AsyncTask<Integer, Integer, String> {
        @Override
        protected void onPostExecute(String s) { }

        @Override
        protected void onProgressUpdate(Integer... values) {
            playProgress_sb.setProgress(values[0]);
        }

        @Override
        protected String doInBackground(Integer... params) {
            while (center_v.isSelected()) {
                this.publishProgress(content_vv.getCurrentPosition());
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            return null;
        }
    }

    protected void toast(String msg) {
        Toast.makeText(this, msg, Toast.LENGTH_SHORT).show();
    }
}